import { cb } from "../util/iteratee";
import pluck from './pluck'
import map from './map'
function sortBy(obj, iteratee, context) {
  var index = 0;
  iteratee = cb(iteratee, context);
  return pluck(map(obj, function (value, key, list) {
    return {
      value: value,
      index: index++,
      criteria: iteratee(value, key, list)
    }
  }).sort(function (left: { value: any, index: number, criteria: Function }, right: { value: any, index: number, criteria: Function }) {
    var a = left.criteria
    var b = right.criteria
    if (a !== b) {
      if (a > b || a === void 0) return 1
      if (a < b || b === void 0) return -1
    }
    return left.index = right.index
  }), 'value')
}

export default sortBy